#include <assert.h>

#include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

class MyStack {
 public:
  MyStack() : queue_ref_(&queue_)
  {
    //
  }

  void push(int x)
  {
    queue_ref_->push(x);
  }

  int pop()
  {
    std::queue<int> *other = (queue_ref_ == &queue_) ? &queue1_ : &queue_;

    int sz = queue_ref_->size();
    for (int i = 0; i != sz - 1; ++i)
    {
      other->push(queue_ref_->front());
      queue_ref_->pop();
    }
    int val = queue_ref_->front();
    queue_ref_->pop();

    queue_ref_ = other;

    return val;
  }

  int top()
  {
    std::queue<int> *other = (queue_ref_ == &queue_) ? &queue1_ : &queue_;

    int sz = queue_ref_->size();
    for (int i = 0; i != sz - 1; ++i)
    {
      other->push(queue_ref_->front());
      queue_ref_->pop();
    }
    int val = queue_ref_->front();
    queue_ref_->pop();
    other->push(val);

    queue_ref_ = other;

    return val;
  }

  bool empty()
  {
    return queue1_.empty() && queue_.empty();
  }

  std::queue<int> *queue_ref_;
  std::queue<int> queue_;
  std::queue<int> queue1_;
};

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack* obj = new MyStack();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->top();
 * bool param_4 = obj->empty();
 */
